package dao.impl;

import dao.UserDao;
import pojo.User;
import service.UserService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAll() {
        //TODO
        //jdbc操作数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //1、 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "azzzzzq123");
            //输入新增的数据
            String sql = "insert into user (id ,name ,pass) values(?,?,?)";

            //4.预编译sql

            //5设置参数

            //6.执行增加
            int result = preparedStatement.executeUpdate();
            if (result>0){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {0,
            //7.关闭资源:从后往前依次关闭
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        return null;
    }
}

